package com.atguigu.yygh.user.controller.user;


import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.util.JwtHelper;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.service.PatientService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import java.util.List;

/**
 * <p>
 * 就诊人表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-09-03
 */
@RestController
@RequestMapping("/user/userinfo/patient")
@Api(tags = {"就诊人信息管理"})
public class PatientController {

    @Autowired
    private PatientService patientService;

    @ApiOperation(value = "远程调用--根据就诊人id获取就诊人信息")
    @GetMapping("/{patientId}")
    public Patient getPatientById(@PathVariable("patientId") Long patientId) {
        Patient patient = patientService.getById(patientId);
        return patient;
    }

    // 增
    @ApiOperation(value = "增加就诊人")
    @PostMapping("/save")
    public R save(@RequestBody Patient patient, @RequestHeader String token) {
        Long userId = JwtHelper.getUserId(token);
        patient.setUserId(userId);
        patientService.save(patient);
        return R.ok();
    }
    // 删
    @ApiOperation(value = "删除就诊人")
    @DeleteMapping("/delete/{id}")
    public R remove(@PathVariable("id")Long id) {
        patientService.removeById(id);
        return R.ok();
    }
    // 改
    // 1.修改数据-回显
    @ApiOperation(value = "修改数据-回显")
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable Long id) {
        Patient patient = patientService.detail(id);
        return R.ok().data("patient", patient);
    }
    // 2. 修改数据
    @ApiOperation(value = "修改就诊人数据")
    @PutMapping("/update")
    public R update(@RequestBody Patient patient) {
        patientService.updateById(patient);
        return R.ok();
    }
    // 查
    @ApiOperation(value = "查询所有就诊人数据")
    @GetMapping("/all")
    public R getAll(@RequestHeader String token) {

        List<Patient> list = patientService.findAllPatient(token);
        return R.ok().data("list", list);
    }
}

